package springboot.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import springboot.bean.*;
import springboot.http.HttpClientTools;
import springboot.kernel.KernelTools;
import springboot.mapper.StackMapper;
import springboot.mapper.StackTaskMapper;
import springboot.websocket.WebSocketServer;

import java.io.IOException;
import java.util.List;

@Controller
public class StackController {

    @Autowired
    StackMapper stackMapper;
    @Autowired
    StackTaskMapper stackTaskMapper;

    /*将所有的变量放在外面，方便JVM垃圾回收,稍微优化点性能*/
    KernelTools kernelTools=null;
    StackExample ex=null;
    Stack stack1=null;
    StackExample.Criteria criteria=null;
    Stack stack=null;
    List<Stack> list=null;

    StackTaskExample taskExample=null;
    StackTaskExample.Criteria taskCriteria=null;
    StackTask stackTask=null;
    List<StackTask> taskList=null;

    @RequestMapping(value = "/showAllStack",method = RequestMethod.GET)
    @ResponseBody
    public void selectAllStack() throws IOException {
        ex=new StackExample();
        list=stackMapper.selectByExample(ex);
        WebSocketServer.sendInfo(list);
    }

    @RequestMapping(value = "/clear",method = RequestMethod.POST)
    @ResponseBody
    public void clearByIndex(@RequestParam(required = true,value = "index")String index) throws IOException {
        ex=new StackExample();
        criteria=ex.createCriteria();
        criteria.andIndexEqualTo(index);
        stack=new Stack();
        stack.setStatus("NO");
        stackMapper.updateByExampleSelective(stack,ex);
        HttpClientTools.sendGetData("http://localhost:9998/showAllStack","utf-8");
        /*筛选出该index对应的line*//*让line和最大不能用的maxLine做比对*/
        ex=new StackExample();
        criteria=ex.createCriteria();
        criteria.andIndexEqualTo(index);
        list=stackMapper.selectByExample(ex);
        stack=list.get(0);
        if (Integer.parseInt(stack.getLine())<Integer.parseInt(stackMapper.selectMaxLineINNO(stack.getPosition()).getMaxLine())){
            System.out.println("对不起,只有释放外层的才能产生任务");
        }else if (Integer.parseInt(stack.getLine())==1||Integer.parseInt(stack.getLine())==2){
            System.out.println("释放货位，但是目前没有任务");
        }else{
            taskExample=new StackTaskExample();
            taskCriteria=taskExample.createCriteria();
            taskCriteria.andStatusEqualTo("0");
            taskCriteria.andToEqualTo("60"+stack.getPosition().substring(2));
            taskList=stackTaskMapper.selectByExample(taskExample);
            if(taskList.size()==0){
                System.out.println("没有缓存的任务");
            }else{
                taskList.get(0).setStatus("1");
                stackTaskMapper.updateByPrimaryKey(taskList.get(0));
                System.out.println("位置释放,生成其它任务");
            }
        }

    }

    @RequestMapping(value = "/forbid",method = RequestMethod.POST)
    @ResponseBody
    public void forbidByIndex(@RequestParam(required = true,value = "index")String index) throws IOException {
        ex=new StackExample();
        criteria=ex.createCriteria();
        criteria.andIndexEqualTo(index);
        stack=new Stack();
        stack.setStatus("NOUSE");
        stackMapper.updateByExampleSelective(stack,ex);
        HttpClientTools.sendGetData("http://localhost:9998/showAllStack","utf-8");
    }

    /*
     *形参前面加@RequestBody用于解析json类型的请求数据
     * Content-Type：application/json[请求头一定要是这样]
     * */
    /*如果没有@RequestBody,那么只能够接收表单形式的数据[如下所示]*/
    /*网址：http://localhost:9998/test?name=ss&age=24*/
    /*请求方法：POST*/
    @RequestMapping(value = "/creatOrder",method = RequestMethod.POST)
    @ResponseBody
    public void creatOrderByHttp(@RequestBody Order order) throws IOException {
        /*逻辑大概就是这样滴*/
        /*
        * 1.上位只发到哪一列地堆，并不发送具体到哪一个
        * 2.一列地堆只发送一条相关的任务(任务发下来，我做持久化缓存到数据库中，直到这一列任务完成，再发送任务给车辆)
        * 3.任务完成后让地堆显示变为红色，同时让其它车辆可以接收相关列的任务
        * 4.创建任务要看该列是否有正在使用的货位，以及该列是否有可以使用的货位
        * */
        /*查找出该列不能使用的最大行*/
        String start=order.getDestinations().get(0).getLocationName();
        String end=order.getDestinations().get(1).getLocationName();
        String orderName=order.getProperties().get(0).getValue1();
        stack=stackMapper.selectMaxLineINNO(end);
        if(stack.getMaxLine().equals("0")){//表明该列没有货物
            kernelTools=new KernelTools();
            kernelTools.CreateOrderOneStation(start,"80"+end.substring(2),orderName);
            /*改变任务站点*/
            ex=new StackExample();
            criteria=ex.createCriteria();
            criteria.andPositionEqualTo("80"+end.substring(2));
            stack1=new Stack();
            stack1.setStatus("TOUSE");
            stackMapper.updateByExampleSelective(stack1,ex);
            HttpClientTools.sendGetData("http://localhost:9998/showAllStack","utf-8");
            System.out.println("最里面的任务发送成功");
        }else if(stack.getMaxLine().equals("1")){//表明该列有一个货物(并且该货物不是TOUSE)
            /*查询80的该货物是否是TOUSE*/
            ex=new StackExample();
            criteria=ex.createCriteria();
            criteria.andPositionEqualTo("80"+end.substring(2));
            criteria.andStatusEqualTo("TOUSE");
            list=stackMapper.selectByExample(ex);
            if (list.size()==0){
                kernelTools=new KernelTools();
                kernelTools.CreateOrderOneStation(start,"70"+end.substring(2),orderName);
                /*改变任务站点*/
                ex=new StackExample();
                criteria=ex.createCriteria();
                criteria.andPositionEqualTo("70"+end.substring(2));
                stack1=new Stack();
                stack1.setStatus("TOUSE");
                stackMapper.updateByExampleSelective(stack1,ex);
                HttpClientTools.sendGetData("http://localhost:9998/showAllStack","utf-8");
                System.out.println("中间的任务发送成功");
            }else{
                stackTask=new StackTask();
                stackTask.setFrom(start);
                stackTask.setTo("60"+end.substring(2));
                stackTask.setStatus("0");
                stackTaskMapper.insertSelective(stackTask);
                System.out.println("第一个货位正在使用中,已为您安排到数据库中,请等待");
            }
        }else if(stack.getMaxLine().equals("2")){//表明该列有两个货物(并且没有TOUSE)
            /*查询70的该货物是否是TOUSE*/
            ex=new StackExample();
            criteria=ex.createCriteria();
            criteria.andPositionEqualTo("70"+end.substring(2));
            criteria.andStatusEqualTo("TOUSE");
            list=stackMapper.selectByExample(ex);
            if(list.size()==0){
                kernelTools=new KernelTools();
                kernelTools.CreateOrderOneStation(start,"60"+end.substring(2),orderName);
                /*改变任务站点*/
                ex=new StackExample();
                criteria=ex.createCriteria();
                criteria.andPositionEqualTo("60"+end.substring(2));
                stack1=new Stack();
                stack1.setStatus("TOUSE");
                stackMapper.updateByExampleSelective(stack1,ex);
                HttpClientTools.sendGetData("http://localhost:9998/showAllStack","utf-8");
                System.out.println("最外面的任务发送成功");
            }else{
                stackTask=new StackTask();
                stackTask.setFrom(start);
                stackTask.setTo("60"+end.substring(2));
                stackTask.setStatus("0");
                stackTaskMapper.insertSelective(stackTask);
                System.out.println("第二个货位正在使用中,已为您安排到数据库中,请等待");
            }
        }else if(stack.getMaxLine().equals("3")){//表明该列有三个货物(堆满了)
            stackTask=new StackTask();
            stackTask.setFrom(start);
            stackTask.setTo("60"+end.substring(2));
            stackTask.setStatus("0");
            stackTaskMapper.insertSelective(stackTask);
            System.out.println("对不起，该列的库位已经满了,请及时清空,已为您安排到数据库中");
        }

    }
}
